import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_navigation/get_navigation.dart';
import 'package:ljy_flutter/get/obxCountExample/obx_teacher.dart';

class ObxCustomClass extends StatelessWidget {
  const ObxCustomClass({super.key});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'obx custome class demo',
      home: ObxCustomWidget(),
    );
  }
}

class ObxCustomWidget extends StatelessWidget {
  ObxCustomWidget({super.key});

  var teacher = ObxTeacher();

  var student = ObxStudent(name: '樱木花道', age: 18).obs;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('obx custome class demo')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            // 第一种方式
            Obx(
              () => Text(
                '老师名称：${teacher.name.value}',
                style: TextStyle(color: Colors.red, fontSize: 30),
              ),
            ),
            Obx(
              () => Text(
                '老师年龄：${teacher.age.value}',
                style: TextStyle(color: Colors.red, fontSize: 30),
              ),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                teacher.name.value = teacher.name.value.toUpperCase();
                teacher.age.value = teacher.age.value + 1;
              },
              child: Text('+++'),
            ),
            SizedBox(height: 20),
            Obx(
              () => Text(
                '学生名称：${student.value.name}',
                style: TextStyle(color: Colors.red, fontSize: 30),
              ),
            ),
            Obx(
              () => Text(
                '学生年龄：${student.value.age}',
                style: TextStyle(color: Colors.red, fontSize: 30),
              ),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                student.update((stu) {
                  student.value.name = '流川枫';
                  student.value.age++;
                });
              },
              child: Text('+++'),
            ),
          ],
        ),
      ),
    );
  }
}
